

package com.kimo.shop.sys.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kimo.shop.common.config.Constant;
import com.kimo.shop.common.i18n.I18nMessage;
import com.kimo.shop.sys.dao.SysMenuMapper;
import com.kimo.shop.sys.dao.SysRoleMenuMapper;
import com.kimo.shop.sys.model.SysMenu;
import com.kimo.shop.sys.service.SysMenuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author kimo-taozhouchuan
 */
@Service("sysMenuService")
@AllArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

	private final SysRoleMenuMapper sysRoleMenuMapper;

	private final SysMenuMapper sysMenuMapper;

	@Override
	public List<SysMenu> listMenuByUserId(Long userId) {

		// 用户的所有菜单信息
		List<SysMenu> sysMenus ;
		//系统管理员，拥有最高权限
		if(userId == Constant.SUPER_ADMIN_ID){
			sysMenus = sysMenuMapper.listMenu(I18nMessage.getLang());
		}else {
			sysMenus = sysMenuMapper.listMenuByUserId(userId,I18nMessage.getLang());
		}

		Map<Long, List<SysMenu>> sysMenuLevelMap = sysMenus.stream()
				.sorted(Comparator.comparing(SysMenu::getOrderNum))
				.collect(Collectors.groupingBy(SysMenu::getParentId));

		// 一级菜单
		List<SysMenu> rootMenu = sysMenuLevelMap.get(0L);
		if (CollectionUtil.isEmpty(rootMenu)) {
			return Collections.emptyList();
		}
		// 二三级菜单
		for (SysMenu sysMenu : rootMenu) {
			List<SysMenu> secondList = sysMenuLevelMap.get(sysMenu.getMenuId());
			if (CollectionUtil.isEmpty(secondList)){
				sysMenu.setList(secondList);
				continue;
			}
			for (SysMenu secondMenu : secondList) {
				secondMenu.setList(sysMenuLevelMap.get(secondMenu.getMenuId()));
			}
			sysMenu.setList(secondList);
		}
		return rootMenu;
	}

	@Override
	public void deleteMenuAndRoleMenu(Long menuId){
		//删除菜单
		this.removeById(menuId);
		//删除菜单与角色关联
		sysRoleMenuMapper.deleteByMenuId(menuId);
	}


	@Override
	public List<Long> listMenuIdByRoleId(Long roleId) {
		return sysMenuMapper.listMenuIdByRoleId(roleId);
	}


	@Override
	public List<SysMenu> listSimpleMenuNoButton() {
		return sysMenuMapper.listSimpleMenuNoButton(I18nMessage.getLang());
	}

	@Override
	public List<SysMenu> listRootMenu() {
		return sysMenuMapper.listRootMenu(I18nMessage.getLang());
	}

	@Override
	public List<SysMenu> listChildrenMenuByParentId(Long parentId) {
		return sysMenuMapper.listChildrenMenuByParentId(parentId,I18nMessage.getLang());
	}

	@Override
	public List<SysMenu> listMenuAndBtn() {
		return sysMenuMapper.listMenuAndBtn(I18nMessage.getLang());
	}

}
